<?php
if($att['type'] == 3)
{
	$valuefind = false;
	$uploaded = false;
	$uploadValid = false;
	$uploadInvalid = 0;
	
	$uploadedImage['search'] = '';
	
	$uploadedImage['filename'] = '';
	$uploadedImage['url'] = '';
	$uploadedImage['thumb'] = '';
	$uploadedImage['ext'] = '';
	
	if($_FILES['a_'.$att['id'].'_v_upload']) 
	{
		$images=0;
		$file = $_FILES['a_'.$att['id'].'_v_upload'];
		if($file["name"] != '')
		{
			$imageClass = new Image();
			switch($file['type'])
			{
				case 'image/pjpeg':
				case 'image/jpeg':
					if($imageClass->image_valid($file['type']) && ($file['name'] != ""))
					{
						if($file['size'] < 5000000)
						{ 
							$error['a_'.$att['id'].'_v_upload'] = $cart->uploadCartImage($file, $shop->CurrentProduct->getId()); 
							$valuefind = true; 
						}
						else
						{ 
							$error['a_'.$att['id'].'_v_upload'] = 'Bildet er st&oslash;rre enn 5MB'; 
							$uploadInvalid = 1;
						}
					}
					else
					{ 
						$error['a_'.$att['id'].'_v_upload'] = ucfirst($txt->getWord('invalid').' '.$txt->getWord('file')); 
						$uploadInvalid = 1;
					}
				break;
				default:
					$error['a_'.$att['id'].'_v_upload'] = 'Ugyldig format. <br />Kun JPG bilder tillatt.'; 
					$uploadInvalid = 1;
				break;
			}
		}
	}
	
	$uploadedImage['search'] = $user->getSessionId().'_'.$shop->CurrentProduct->getId();
	
	if(is_file(CART_UPLOAD_PATH.'thumbs/'.$uploadedImage['search'].'.jpg'))
	{ $uploadedImage['ext'] = '.jpg'; }
	if(is_file(CART_UPLOAD_PATH.'thumbs/'.$uploadedImage['search'].'.png'))
	{ $uploadedImage['ext'] = '.png'; }
	if(is_file(CART_UPLOAD_PATH.'thumbs/'.$uploadedImage['search'].'.gif'))
	{ $uploadedImage['ext'] = '.gif'; }
	if($uploadedImage['ext'] != '')
	{
		$uploadedImage['url'] = CART_UPLOAD_URL.$uploadedImage['search'].$uploadedImage['ext'];
		$uploadedImage['thumb'] = CART_UPLOAD_URL.'thumbs/'.$uploadedImage['search'].$uploadedImage['ext'];
		$uploadedImage['filename'] = $uploadedImage['search'].$uploadedImage['ext'];
	}
	
	if((intval($_POST['a_'.$att['id'].'_v_delete_uploaded']) > 0) || ($uploadInvalid > 0))
	{
		if($uploadedImage['url'] != '')
		{
			$filename = $uploadedImage['search'].$uploadedImage['ext'];
			if(is_file(CART_UPLOAD_PATH.'thumbs/'.$filename))
			{ unlink(CART_UPLOAD_PATH.'thumbs/'.$filename); }
			if(is_file(CART_UPLOAD_PATH.$filename))
			{ unlink(CART_UPLOAD_PATH.$filename); }
			
			$valuefind = false;
			$uploaded = false;
			$uploadValid = false;
			$uploadedImage['url'] = '';
			$uploadedImage['ext'] = '';
			$uploadedImage['thumb'] = '';
			$uploadedImage['filename'] = '';
		}
	}
	
	$trueValId = 0;
	$falseValId = 0;
	$curValId = 0;
	
	if(count($shop->getProductAttribute($att['id'])->getValues()) > 1)
	{
		foreach($shop->getProductAttribute($att['id'])->getValues() as $val)
		{
			if(($val->getActive()) && (is_object($shop->CurrentProduct->getVariationByAttributeValueId($att['id'], $val->getId()))))
			{
				if(($falseValId == 0) || ($trueValId == 0))
				{
					if(($val->getValue() != '') && 
					   (strtolower($val->getValue()) != 'no') && 
					   (strtolower($val->getValue()) != 'nei') && 
					   (strtolower($val->getValue()) != 'ne') && 
					   (strtolower($val->getValue()) != 'none') && 
					   (strtolower($val->getValue()) != 'ingen') && 
					   (strtolower($val->getValue()) != 'nema') && 
					   (strtolower($val->getValue()) != 'false'))
					{ $trueValId = $val->getId(); }
					else
					{ $falseValId = $val->getId(); }
				}
			}
		}
		$curValId = $falseValId;
		if($uploadedImage['url'] != '')
		{ $curValId = $trueValId; }
	} 
	?>
	<input type="hidden" id="a_<?php echo $att['id']; ?>_v" name="a_<?php echo $att['id']; ?>_v" value="<?php echo intval($curValId); ?>"  />
	<div class="form-group">
		<label for="a_<?php echo $att['id']; ?>_v" class="control-label"><?php echo $att['title']; ?>: 190.00 kr.</label>
		<?php if($uploadedImage['url'] != ''): ?>
			<?php echo ucfirst($txt->getWord('image')); ?> p&aring; kaken <br />
			<a class="fancyBox" href="<?php echo $uploadedImage['url']; ?>"><img src="<?php echo CART_UPLOAD_URL.'thumbs/view.php?image='.$uploadedImage['filename'].'&amptime='.time(); ?>" alt="Opplastet Bilde Klikk for original versjon" /></a>
			<br />
			<input type="hidden" id="a_<?php echo $att['id']; ?>_v_delete_uploaded" name="a_<?php echo $att['id']; ?>_v_delete_uploaded" value="0" />
			<a onclick="deleteUploaded();" href="#" id="delete_uploaded" class="btn btn-warning btn-block"><i class="fa fa-trash-o"></i> Slett bilde</a>
			<hr />
		<?php else:  ?>
			<input type="file" id="a_<?php echo $att['id']; ?>_v_upload" name="a_<?php echo $att['id']; ?>_v_upload" class="input-controll" />
			<span class="help-block">( maks 5 MB i JPG format )</span>
			<?php if($error['a_'.$att['id'].'_v_upload'] != '') { echo '<br /><span class="error has-error">'.$error['a_'.$att['id'].'_v_upload'].'</span>'; } ?>
			<a class="btn btn-default btn-block" onclick="<?php echo 'setAttUploadValue('.$att['id'].', 3, \''.$trueValId.'\', \''.$falseValId.'\', \''.$curValId.'\', 0);' ?>"><i class="fa fa-cloud-upload"></i> <?php echo ucfirst($txt->getWord('add_image')); ?></a>
		<?php endif;  ?>
	</div>
	<script type="text/javascript" language="javascript">
	<?php if(($curValId == $trueValId) && ($uploadedImage['url'] != '')): ?>
	//Check if correct variationId is set
	jQuery(document).ready(function(){ checkValues(<?php echo $att['id']; ?>); });
	<?php else: ?>
	jQuery('#a_<?php echo $att['id']; ?>_v_upload').change(function() 
	{ setAttUploadValue(<?php echo $att['id']; ?>, 3, <?php echo $trueValId; ?>, <?php echo $falseValId; ?>, <?php echo $curValId; ?>, 0); });
	<?php endif; ?>
	function deleteUploaded()
	{
		jQuery('#a_<?php echo $att['id']; ?>_v').val(<?php echo intval($falseValId); ?>);
		jQuery('#a_<?php echo $att['id']; ?>_v_delete_uploaded').val(1);
		jQuery('input#attribute_<?php echo $att['id']; ?>_value').val(<?php echo intval($falseValId); ?>);
		pageLoading();
		jQuery('form#att_select_form').submit();
	}
	<?php if($uploadInvalid > 0): ?>
	jQuery(document).ready(function(){ deleteUploaded(); });
	<?php endif; ?>
	</script>
	<?php
	unset($trueValId);
	unset($falseValId);
	unset($curValId);
	unset($valuefind);
	unset($uploaded);
	unset($uploadValid);
	unset($uploadedImage);
}
?>